import plugin from '../../../lib/plugins/plugin.js'
import fs from 'fs'
import lodash from 'lodash'
import puppeteer from '../../../lib/puppeteer/puppeteer.js';
import cfg from '../../../lib/config/config.js'
import { Common, Data, Version, Plugin_Name, Plugin_Path, Config } from '../components/index.js'
const botname = global.Bot.nickname;
import {createRequire} from "module";
const require = createRequire(import.meta.url);

export class JiaoYehelp extends plugin {
constructor() {
super({
name: '帮助',
dsc: '',
event: 'message',
priority: 100,
rule: [
{
reg: `^#?(${botname})?(附加|额外|沙盒)?(help|菜单|帮助)$`,
fnc: 'default'
},
{
reg: "^#?(${botname})?(map|地图)(菜单|帮助|列表|help)$",
fnc: 'map'
}
]
});
}

async default() {
    return await help(this.e,"help");
  }
  async map() {
let set = await Config.admin_set
let isplugin = set.map
if (!isplugin) return false
    return await help(this.e,"maphelp");
  }
}

async function help(e,key) {
  let custom = {}
  let help = {}
  let { diyCfg, sysCfg } = await Data.importCfg(key)
  custom = help
  let helpConfig = lodash.defaults(diyCfg.helpCfg || {}, custom.helpCfg, sysCfg.helpCfg)
  let helpList = diyCfg.helpList || custom.helpList || sysCfg.helpList
  let helpGroup = []
  lodash.forEach(helpList, (group) => {
    if (group.auth && group.auth === 'master' && !e.isMaster) {
      return true
    }
    lodash.forEach(group.list, (help) => {
      let icon = help.icon * 1
      if (!icon) {
        help.css = 'display:none'
      } else {
        let x = (icon - 1) % 10
        let y = (icon - x - 1) / 10
        help.css = `background-position:-${x * 50}px -${y * 50}px`
      }
    })
    helpGroup.push(group)
  })
  let bg = await rodom()
  let colCount = 3;
  return await Common.render('help/index', {
    helpCfg: helpConfig,
    helpGroup,
    bg,
    colCount,
  }, {
    e,
    scale: 2.0
  })
}

const rodom = async function () {
  var image = fs.readdirSync(`./plugins/js-box-plugin/resources/help/imgs/`);
  var list_img = [];
  for (let val of image) {
    list_img.push(val)
  }
  var imgs = list_img.length == 1 ? list_img[0] : list_img[lodash.random(0, list_img.length - 1)];
  return imgs;
}